package com.balance.anole.erp.navigate.view;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.NotEnabledException;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.core.commands.common.NotDefinedException;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ExpandBar;
import org.eclipse.swt.widgets.ExpandItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;

import com.balance.anole.common.to.NavigateTO;
import com.balance.anole.erp.security.SessionManager;
import com.balance.anole.erp.security.login.model.RetailLoginUser;

import edu.emory.mathcs.backport.java.util.Collections;

public class NewNavigationView extends ViewPart {
	public static final String ID = "com.balance.anole.erp.NewNavigationView";


	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		initiallizeExpandBar(parent);
		initializeToolBar();
	}

	private void initiallizeExpandBar(Composite parent) {

		//get navigate by login user
		RetailLoginUser rlu = SessionManager.getInstance().getRetailLoginUser();

		Set<NavigateTO> navigates = rlu.getNavigates();

		Map<NavigateTO, List<NavigateTO>> navigateBarLink = new HashMap<NavigateTO, List<NavigateTO>>();

		List<NavigateTO> navigateBar = new ArrayList<NavigateTO>();
		for(Iterator<NavigateTO> it = navigates.iterator();it.hasNext();){
			NavigateTO navigateTO = (NavigateTO)it.next();
			if(NavigateTO.TYPE_EXPAND.equals(navigateTO.getType())){
				navigateBar.add(navigateTO);
			}
		}

		for(int i=0;i<navigateBar.size();i++){
			List<NavigateTO> navigateLink = new ArrayList<NavigateTO>();
			for(Iterator<NavigateTO> it = navigates.iterator();it.hasNext();){
				NavigateTO navigateTO = (NavigateTO)it.next();
				if(navigateTO.getParentNavigate()!=null){
					if(navigateBar.get(i).getFunId().equals(navigateTO.getParentNavigate().getFunId())){
						navigateLink.add(navigateTO);
					}
				}
			}
			navigateBarLink.put(navigateBar.get(i), navigateLink);
		}

		ExpandBar expandBar = new ExpandBar(parent, SWT.V_SCROLL);
		//expandBar.setBackground(new Color(parent.getDisplay(), 255, 255, 255));
		expandBar.setSpacing(1);

		//sorting by sn.
		Comparator<NavigateTO> menuComparator = new Comparator<NavigateTO>(){

			@Override
			public int compare(NavigateTO o1, NavigateTO o2) {
				return o2.getSn() - o1.getSn();
			}

		};
		List<NavigateTO> sortedMenuGroup = new ArrayList<NavigateTO>(navigateBarLink.keySet());
		Collections.sort(sortedMenuGroup, menuComparator);
		//change use permission navigate
		Iterator<NavigateTO> navigateTOIterator = sortedMenuGroup.iterator();
		while(navigateTOIterator.hasNext()){
			NavigateTO navigateBarTO = navigateTOIterator.next();
			List<NavigateTO> NavigateListTOs = navigateBarLink.get(navigateBarTO);
			//sorting by sn
			Collections.sort(NavigateListTOs, menuComparator);
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(navigateBarTO.getFunDesc());
			expandItem.setHeight(120);
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(navigateBarTO.getIconPath()).createImage());

			LinkList linkList = new LinkList(expandBar);
			for(int i=0;i<NavigateListTOs.size();i++){
				linkList.addLink(NavigateListTOs.get(i).getIconPath(),NavigateListTOs.get(i).getFunDesc(), NavigateListTOs.get(i).getCommand());
				expandItem.setControl(linkList.getControl());
			}
		}



		/*

		{//商品基础设置
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_ROOT);
			expandItem.setHeight(120);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.PRODUCT_MAINT).createImage());

			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_BRAND, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_TYPE, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PC, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_COLOR, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_UNKNOW, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SIZE, null);
			expandItem.setControl(linkList.getControl());
			//expandItem.getControl().setBackground(new Color(parent.getDisplay(), 255,255,255));
		}
		{//商品
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_ROOT2);
			expandItem.setHeight(120);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.PRODUCTS).createImage());
			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PDT, ICommandIds.CMD_NAVIGATE_PRODUCTMAINTAIN);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SCH_PDT, ICommandIds.CMD_NAVIGATE_PRODUCTSEARCH);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SUPPLIES, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_PRT_SCUTCHEON, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_GEN_SERIAL_NO, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_PRODUCT_MAINT_SERIAL_NO, null);
			expandItem.setControl(linkList.getControl());
		}
		{//订货管理
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_ORDER_ROOT);
			expandItem.setHeight(135);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.ORDERS).createImage());
			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_PREORDER_ROOT, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_PREORDER_FDBK_ROOT, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT, ICommandIds.CMD_NAVIGATE_ORDERMAINTAIN);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_REPORT, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_ORDER_PLAN, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_ORDER_PLAN_RPT, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_VIEW_PDT_ASSIGNMENT, null);
			expandItem.setControl(linkList.getControl());
		}
		{//商业智能分析
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_REPORT);
			expandItem.setHeight(65);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.REPORTS).createImage());
			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_REPORT, ICommandIds.CMD_REPORT_ORDERANALYZE);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_ORDER_MAINT_VIEW_PDT_ASSIGNMENT, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_REFUND_RPT, null);
			expandItem.setControl(linkList.getControl());
		}

		{//档案管理
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_ROOT);
			expandItem.setHeight(155);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.MGMT_MAINT).createImage());
			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT, ICommandIds.CMD_NAVIGATE_AGENTMAINTAIN);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_STAFF, ICommandIds.CMD_NAVIGATE_STAFFMAINTAIN);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_RETAG, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_AUTH, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_GRP, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_AGENT_GRP2, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_STAFF_GRP, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_MGMT_MAINT_VIEW_AGENT_GRP, null);
			expandItem.setControl(linkList.getControl());
		}
		{//系统维护
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_SYS_MAINT_ROOT);
			expandItem.setHeight(135);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_MAINT).createImage());
			LinkList linkList = new LinkList(expandBar);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_INI_SYS, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_GEN_RT_DATA, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_CONFIG_RT_DATA, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_BKUP_DB, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_RECV_DB, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_EXP_DB, null);
			linkList.addLink(IIcons.TREE_ITEM,Messages.LEFT_MENU_VIEW_SYS_MAINT_IMP_DB, null);
			expandItem.setControl(linkList.getControl());
		}
		{//系统用户
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_USER);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.USERS).createImage());
		}
		{//系统配置
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_SYS_CONFIG);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_SETTING).createImage());
		}
		{//系统权限
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_AUTH);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_ACC_RIGHT).createImage());
		}
		{//系统监控
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_AT);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_MONITOR).createImage());
		}

		{//系统消息
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_MSG);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_MESSAGE).createImage());
		}
		{//系统公告
			ExpandItem expandItem = new ExpandItem(expandBar, SWT.NONE);
			expandItem.setText(Messages.LEFT_MENU_VIEW_MGMT_MAINT_BILLBOARD);
			expandItem.setHeight(0);// 设置Item的高度
			expandItem.setImage(com.balance.anole.erp.Activator.getImageDescriptor(IIcons.SYS_BILLBOARD).createImage());
		}
		*/
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
	}

	private void initializeToolBar() {
		IToolBarManager toolBarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	class LinkList {
		private Composite comp;

		public LinkList(Composite parent) {
			comp = new Composite(parent, SWT.NONE);
			GridLayout layout = new GridLayout(2, false);
			layout.marginLeft = 15;
			comp.setLayout(layout);
		}

		public void addLink(String imagePath, String text, String commandId) {
			if (StringUtils.isNotBlank(imagePath)) {
				new Label(comp, SWT.NONE).setImage(com.balance.anole.erp.Activator.getImageDescriptor(imagePath).createImage());
			}
			Link link = new Link(comp, SWT.NONE);
			link.setText(text);
//			link.setBackground(null);
		    link.addMouseTrackListener(new MouseTrackAdapter() {
		    	@Override
		    	public void mouseEnter(MouseEvent e) {
		    		((Link)e.getSource()).setFocus();
		    		((Link)e.getSource()).setCursor(new Cursor(null,SWT.CURSOR_HAND));
		    	}
		    });
			if (StringUtils.isNotBlank(commandId)) {
				link.setData(commandId);
				link.addMouseListener(new MouseAdapter() {
			    	@Override
			    	public void mouseDown(MouseEvent e) {
			    		onLinkSelected(((Link) e.getSource()).getData().toString());
			    	}
			    });
//				link.addSelectionListener(new SelectionAdapter() {
//					public void widgetSelected(SelectionEvent e) {
//						onLinkSelected(((Link) e.getSource()).getData().toString());
//					}
//				});
			}
		}

		public Composite getControl() {
			return comp;
		}

		private void onLinkSelected(String commandId) {
			IHandlerService hs = (IHandlerService) getSite().getService(
					IHandlerService.class);

			try {
				hs.executeCommand(commandId, null);
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotDefinedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotEnabledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotHandledException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
